<?php

/**
 * 用户相关接口
 */
namespace Pay\Controller;

use Think\Controller;

class PayController extends Controller
{
    function __construct() {
        parent::__construct();
        $this->FalowWater_model=D("Pay/FalowWater");
    }
    public function  AliPayCallBack(){
        
        require_once(dirname(dirname((__FILE__))) . "/Pay/Alipay/" . 'alipay.config.php');
	    require_once(dirname(dirname((__FILE__))) . "/Pay/Alipay/lib/" . 'alipay_notify.class.php');
	
	    $str = "支付宝返回：" . http_build_query($_REQUEST) . "\r\n";
	    //计算得出通知验证结果
	    $alipayNotify = new \AlipayNotify($alipay_config);
	    $verify_result = $alipayNotify->verifyNotify();
	     if ($verify_result) { 
	        //商户订单号
	        $outTradeNo = $_POST['out_trade_no'];
	        //支付宝交易号
	        $tradeNo = $_POST['trade_no'];
	        //交易状态
	        $tradeStatus = $_POST['trade_status'];
	        if ($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
	            $order = $this->FalowWater_model->where("order_number='{$outTradeNo}'")->find();
	            if (is_array($order)) {
	                if ($order['pay_status'] == 1) {
	                    echo "success";
	                    die();
	                }
	                $data['pay_status']= 1;
	                $up = $this->FalowWater_model->where("falowwater_id ={$order['falowwater_id']}")->save($data);
	                if ($up !== false) {
	                    $str = $str . "--修改成功";
	                    \Think\Log::write($str, 'INFO');
	                    echo "success";        //请不要修改或删除
	                } else {
	                    $str = $str . "--修改状态失败：" . $this->FalowWater_model->getLastSql();
	                    \Think\Log::write($str, 'INFO');
	                    echo "fail";        //请不要修改或删除
	                }
	            } else {
	                $str = $str . "--查找订单失败【{$tradeNo}】";
	                \Think\Log::write($str, 'INFO');
	                echo "fail";        //请不要修改或删除
	            } 
	           // echo "success";
	            
	        } else {
	            $str = $str . "--返回状态失败【{$tradeStatus}】";
	            \Think\Log::write($str, 'INFO');
	            echo "fail"; //验证失败
	        }
	     } else {
	        $str = $str . "--验签失败";
	        \Think\Log::write($str, 'INFO');
	        echo "fail"; //验证失败
	    } 
	    exit;
         
    }
}

